﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Globalization;

namespace D3crft.Windows.UserControls {
    /// <summary>
    /// Interaktionslogik für ucItemComparison.xaml
    /// </summary>
    public partial class ucItemComparison : UserControl {


        private Characters.ClassObjects.ocCharacter oBaseCharacter;
        private int iEquippedItem;
        private float fItemOne_BaseDPS;
        private float fItemOne_ItemDPS;
        private float fItemTwo_BaseDPS;
        private float fItemTwo_ItemDPS;

        private Image oChartImage;
        private Label oBaseDPSOneLabel;
        private Label oItemDPSOneLabel;
        private Label oBaseDPSTwoLabel;
        private Label oItemDPSTwoLabel;

        public ucItemComparison() {
            do_Initialize();
            do_Preset();
            do_StartUp();
        }

        private void do_Initialize() {
            this.InitializeComponent();

        }

        private void do_Preset() {
            iEquippedItem = 1;
            fItemOne_BaseDPS = 0.0f;
            fItemOne_ItemDPS = 0.0f;
            fItemTwo_BaseDPS = 0.0f;
            fItemTwo_ItemDPS = 0.0f;
            oChartImage = this.FindName("Image_Chart") as Image;
            oBaseDPSOneLabel = this.FindName("Label_BDPS_One") as Label;
            oItemDPSOneLabel = this.FindName("Label_IDPS_One") as Label;
            oBaseDPSTwoLabel = this.FindName("Label_BDPS_Two") as Label;
            oItemDPSTwoLabel = this.FindName("Label_IDPS_Two") as Label;
        }

        private void do_StartUp() {

        }


        private void do_RedrawChart() {
            DrawingGroup oGroup = new DrawingGroup();
            GeometryDrawing oDrawing;
            float fMaxDPS;
            int iMaxIndex;
            float fStaged;

            do_CheckDPSValues();

            if ((fItemOne_BaseDPS + fItemOne_ItemDPS) > (fItemTwo_BaseDPS + fItemTwo_ItemDPS)) {
                fMaxDPS = fItemOne_BaseDPS + fItemOne_ItemDPS;
                iMaxIndex = 0;
            }
            else {
                fMaxDPS = fItemTwo_BaseDPS + fItemTwo_ItemDPS;
                iMaxIndex = 1;
            }

            if (iEquippedItem > 0) {
                oDrawing = new GeometryDrawing();
                oDrawing.Pen = new Pen(base.FindResource("Brush_BarEndings") as Brush, 1.0);
                oDrawing.Geometry = new LineGeometry(new Point(0.0, 200.5), new Point(75.0, 200.5));
                oGroup.Children.Add(oDrawing);

                // Item One
                oDrawing = new GeometryDrawing();
                oDrawing.Pen = new Pen(base.FindResource("Gradient_BarNormal") as Brush, 14.0);
                fStaged = (fItemOne_BaseDPS / fMaxDPS) * 200.0f;
                oDrawing.Geometry = new LineGeometry(new Point(22.0, 200.0), new Point(22.0, 200.0 - fStaged));
                oGroup.Children.Add(oDrawing);
                oBaseDPSOneLabel.Content = (fItemOne_BaseDPS / 1000.0f).ToString("N1", CultureInfo.InvariantCulture).Replace(",", "") + "k DPS";
                oBaseDPSOneLabel.Margin = new Thickness(-22.0, 300 - (fStaged / 200.0) * 310, 142.0, (fStaged / 200.0) * 310);
                
                

                if (fItemOne_ItemDPS > 0.0f) {
                    // draw item dps bar!
                    oDrawing = new GeometryDrawing();
                    oDrawing.Pen = new Pen(base.FindResource("Gradient_BarHighlighted") as Brush, 14.0);
                    oDrawing.Geometry = new LineGeometry(new Point(22.0, 200.0 - fStaged), new Point(22.0, 200.0 - (fStaged + ((fItemOne_ItemDPS / fMaxDPS) * 200.0))));
                    oItemDPSOneLabel.Content = (fItemOne_ItemDPS / 1000.0f).ToString("N1", CultureInfo.InvariantCulture).Replace(",", "") + "k DPS";
                    oItemDPSOneLabel.Margin = new Thickness(-22.0, 275.0 - ((fStaged + ((fItemOne_ItemDPS / fMaxDPS) * 100.0)) / 200.0) * 300.0, 142.0, 25.0 + ((fStaged + ((fItemOne_ItemDPS / fMaxDPS) * 100.0)) / 200.0) * 300.0);
                    fStaged += (fItemOne_ItemDPS / fMaxDPS) * 200.0f;
                    oGroup.Children.Add(oDrawing);
                    oItemDPSOneLabel.Visibility = System.Windows.Visibility.Visible;
                }
                else {
                    oItemDPSOneLabel.Visibility = System.Windows.Visibility.Hidden;
                }

                oDrawing = new GeometryDrawing();
                oDrawing.Pen = new Pen(base.FindResource("Brush_BarEndings") as Brush, 1.0);
                oDrawing.Geometry = new LineGeometry(new Point(12.0, 200.0 - fStaged), new Point(32.0, 200.0 - fStaged));
                oGroup.Children.Add(oDrawing);


                // Item Two
                oDrawing = new GeometryDrawing();
                oDrawing.Pen = new Pen(base.FindResource("Gradient_BarNormal") as Brush, 14.0);
                fStaged = (fItemTwo_BaseDPS / fMaxDPS) * 200.0f;
                oDrawing.Geometry = new LineGeometry(new Point(53.0, 200.0), new Point(53.0, 200.0 - fStaged));
                oGroup.Children.Add(oDrawing);
                oBaseDPSTwoLabel.Content = (fItemTwo_BaseDPS / 1000.0f).ToString("N1", CultureInfo.InvariantCulture).Replace(",", "") + "k DPS";
                oBaseDPSTwoLabel.Margin = new Thickness(142.0, 300 - (fStaged / 200.0) * 310, -22.0, (fStaged / 200.0) * 310);

                if (fItemTwo_ItemDPS > 0.0f) {
                    // draw item dps bar!
                    oDrawing = new GeometryDrawing();
                    oDrawing.Pen = new Pen(base.FindResource("Gradient_BarHighlighted") as Brush, 14.0);
                    oDrawing.Geometry = new LineGeometry(new Point(53.0, 200.0 - fStaged), new Point(53.0, 200.0 - (fStaged + ((fItemTwo_ItemDPS / fMaxDPS) * 200.0))));
                    oItemDPSTwoLabel.Content = (fItemTwo_ItemDPS / 1000.0f).ToString("N1", CultureInfo.InvariantCulture).Replace(",", "") + "k DPS";
                    oItemDPSTwoLabel.Margin = new Thickness(142.0, 275.0 - ((fStaged + ((fItemTwo_ItemDPS / fMaxDPS) * 100.0)) / 200.0) * 300.0, -22.0, 25.0 + ((fStaged + ((fItemTwo_ItemDPS / fMaxDPS) * 100.0)) / 200.0) * 300.0);
                    fStaged += (fItemTwo_ItemDPS / fMaxDPS) * 200.0f;
                    oGroup.Children.Add(oDrawing);
                    oItemDPSTwoLabel.Visibility = System.Windows.Visibility.Visible;
                }
                else {
                    oItemDPSTwoLabel.Visibility = System.Windows.Visibility.Hidden;
                }

                oDrawing = new GeometryDrawing();
                oDrawing.Pen = new Pen(base.FindResource("Brush_BarEndings") as Brush, 1.0);
                oDrawing.Geometry = new LineGeometry(new Point(43.0, 200.0 - fStaged), new Point(63.0, 200.0 - fStaged));
                oGroup.Children.Add(oDrawing);
            }





                //oDrawing.Pen = new Pen(base.FindResource("Brush_BarEndings") as Brush, 1.0);
                //oDrawing.Geometry = new LineGeometry(new Point((lItemDPS[i] / fMaxDPS) * 100.0, 2.0 + ((double)i * 16.0)), new Point((lItemDPS[i] / fMaxDPS) * 100.0, 13.0 + ((double)i * 16.0)));
               // oGroup.Children.Add(oDrawing);

            this.oChartImage.Source = new DrawingImage(oGroup);
        }

        private void do_CheckDPSValues() {
            if (fItemOne_BaseDPS + fItemOne_ItemDPS == 0.0f) {
                fItemOne_BaseDPS = Analysis.scCharacterAnalysis.GetCharacterDPS(oBaseCharacter);
                fItemOne_ItemDPS = 0.0f;
            }
            if (fItemTwo_BaseDPS + fItemTwo_ItemDPS == 0.0f) {
                fItemTwo_BaseDPS = Analysis.scCharacterAnalysis.GetCharacterDPS(oBaseCharacter);
                fItemTwo_ItemDPS = 0.0f;
            }
        }

        public void SetCharacter(Characters.ClassObjects.ocCharacter oCharacter) {
            this.oBaseCharacter = oCharacter;
            do_RedrawChart();
        }

        public void SetItemOneDPS(float fBaseDPS, float fItemDPS) {
            this.fItemOne_BaseDPS = fBaseDPS;
            this.fItemOne_ItemDPS = fItemDPS;
            do_RedrawChart();
        }

        public void SetItemTwoDPS(float fBaseDPS, float fItemDPS) {
            this.fItemTwo_BaseDPS = fBaseDPS;
            this.fItemTwo_ItemDPS = fItemDPS;
            do_RedrawChart();
        }

        public void SetEquippedItem(int iItemNumber) {
            iEquippedItem = iItemNumber;
        }
    }
}
